

#include "kkgui_app.h"

#include "kkgui_key.h"
#include "kkgui_base.h"

#include "detect_level.h"
#include "i2c_device.h"

/* =============================== */
/* define */



/* =============================== */
/* typedef */


/* =============================== */
/* function statement*/


/* =============================== */
/* variable statement*/

/* =============================== */
/* variable definition*/
const uint8_t boxing[128] = 
{0xFC,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x3F,0x0F,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFC,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x1F,0x01,0xC0,0xF8,0x00,0x0F,0xFF,0xFF,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xC0,0xE0,0xFE,0xFF,0xFF,0xC0,0x00,0x3F,0x07,0x80,0xF0,0xE0,0xC7,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xFF,0xFF,
0x3F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0xE0,0xF8,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x3F};/*"C:\Users\11412\Desktop\bo.bmp",0*/

const uint8_t i2c_ic[128] = 
{0xFC,0xFE,0xFF,0xFF,0xFF,0x7F,0x7F,0x1F,0xCF,0x2F,0x2F,0xEF,0xEF,0xCF,0xAF,0xAF,0xAF,0xAF,0xCF,0xEF,0xEF,0xEF,0xEF,0xCF,0x1F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFE,0xFC,
0xFF,0xFF,0xFF,0xFF,0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0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE3,0xF0,0xF8,0xFF,0x7F,0x3F};/*"C:\Users\11412\Desktop\note.bmp",0*/

const uint8_t setting[128] = 
{0xFC,0xFE,0xFF,0xFF,0x7F,0x3F,0x3F,0x3F,0x7F,0x7F,0x3F,0x0F,0x07,0xE3,0xF3,0xF3,0xF3,0xF3,0xE3,0x07,0x0F,0x3F,0x7F,0x7F,0x3F,0x3F,0x3F,0x7F,0xFF,0xFF,0xFE,0xFC,
0xFF,0xFF,0xE7,0xC1,0x88,0x1E,0x3F,0xFF,0xFE,0xFE,0x3E,0x0F,0xC7,0xE7,0xF3,0xF3,0xF3,0xF3,0xE7,0xC7,0x0F,0x3E,0xFE,0xFE,0xFF,0x3F,0x1E,0x88,0xC1,0xE7,0xFF,0xFF,
0xFF,0xFF,0xE7,0x83,0x11,0x78,0xFC,0xFF,0x7F,0x7F,0x7C,0xF0,0xE3,0xE7,0xCF,0xCF,0xCF,0xCF,0xE7,0xE3,0xF0,0x7C,0x7F,0x7F,0xFF,0xFC,0x78,0x11,0x83,0xE7,0xFF,0xFF,
0x3F,0x7F,0xFF,0xFF,0xFE,0xFC,0xFC,0xFC,0xFE,0xFE,0xFC,0xF0,0xE0,0xC7,0xCF,0xCF,0xCF,0xCF,0xC7,0xE0,0xF0,0xFC,0xFE,0xFE,0xFC,0xFC,0xFC,0xFE,0xFF,0xFF,0x7F,0x3F};/*"C:\Users\11412\Desktop\set.bmp",0*/
	

kkui_menutab_info_s demo_home[]=
{
    {NULL, NULL, ENTER_LIST, home_func, 1, NULL},  // HOME配置
};

kkui_menutab_info_s demo_menu_main[]=
{
    {(char *)"DETECT LEVEL", 	(char *)boxing, 		ACTION_EXE_LOOP_WITH_LIST, 		detect_level_func, 	2, 		NULL}, // 电平检测
    {(char *)"I2C DEVICE", 	    (char *)i2c_ic, 	    ENTER_LIST, 				    i2c_func, 			6, 	    NULL},
    {(char *)"SPI DEVICE", 	    (char *)spi_ic, 	    ACTION_EXE_ONCE, 				NULL, 				NULL, 	NULL},
    {(char *)"VOLTMETER", 		(char *)voltmeter, 		ACTION_EXE_ONCE, 				NULL, 				NULL, 	NULL},
    {(char *)"UART TOOL", 		(char *)uart, 			ACTION_EXE_ONCE, 				NULL, 				NULL, 	NULL},
    {(char *)"NOTE", 			(char *)note, 			ACTION_EXE_ONCE, 				NULL, 				NULL, 	NULL},
    {(char *)"SETTING", 		(char *)setting, 	   	ENTER_LIST, 					NULL, 				5, 		NULL},
};

kkui_menutab_info_s menu_detect_level[]=
{
    {NULL, NULL, ENTER_LIST, NULL, 3, NULL},
};

extern bool gpio_back_dis, gpio_string_dis;
kkui_menutab_info_s menu_detect_level_set[]=
{
    {(char *)"Channel pull set", 	NULL,	ENTER_LIST, 	gpio_pull_init_func, 	4, 	NULL},
    {(char *)"Back dis", 			NULL, 	SINGLE_CHOICE, 	NULL, 					0, 	&gpio_back_dis},
    {(char *)"String dis", 			NULL, 	SINGLE_CHOICE, 	NULL, 					0, 	&gpio_string_dis},
};


kkui_menutab_info_s menu_detect_level_channel_set[]=
{
    {(char *)"Channe 1", NULL, VALUE_SETTINGS, gpio_pull_set_func, 0, NULL},
    {(char *)"Channe 2", NULL, VALUE_SETTINGS, gpio_pull_set_func, 0, NULL},
    {(char *)"Channe 3", NULL, VALUE_SETTINGS, gpio_pull_set_func, 0, NULL},
    {(char *)"Channe 4", NULL, VALUE_SETTINGS, gpio_pull_set_func, 0, NULL},
    {(char *)"Channe 5", NULL, VALUE_SETTINGS, gpio_pull_set_func, 0, NULL},
    {(char *)"Channe 6", NULL, VALUE_SETTINGS, gpio_pull_set_func, 0, NULL},
    {(char *)"Channe 7", NULL, VALUE_SETTINGS, gpio_pull_set_func, 0, NULL},
    {(char *)"Channe 8", NULL, VALUE_SETTINGS, gpio_pull_set_func, 0, NULL},
};

kkui_menutab_info_s i2c_menu_list[]=
{
    {(char *)"I2C scan", 	    NULL, 	ACTION_EXE_LOOP, 	i2c_scan_func,      0, 	NULL},
    {(char *)"PIN config", 	    NULL, 	ACTION_EXE_ONCE, 	NULL,               0, 	NULL},
    {(char *)"Device demo", 	NULL, 	ENTER_LIST, 	    NULL,               7, 	NULL},
};

kkui_menutab_info_s i2c_demo_list[]=
{
    {(char *)"VKL060", 	        NULL, 	ACTION_EXE_LOOP, 	vkl060_demo_func,      0, 	NULL},
};

kkui_menutab_info_s menu_setting_list[]=
{
    {(char *)"-", 					NULL, 	GO_BACK_LIST, 		NULL, 					0, 	NULL},
    {(char *)"Screen Extinction", 	NULL, 	ACTION_EXE_LOOP, 	screen_extinction_func, 0, 	NULL},
};

// 各个菜单的信息，作为uiMenuInit的输入参数
kkui_menu_info_s demo_menu[] =
{
    {
        .menu_id = 0,           // HOME id 请无比设置为0
        .menu_type = LIST_HOME, // 由于HOME页面特殊，所以会循环执行成员中第一项的函数
        .tab_num = 1,           // HOME界面强制为1
        .member = demo_home
    },
    {
        .menu_id = 1,		// 大菜单
        .menu_type = LIST_ICON,
        .tab_num = 7,
        .member = demo_menu_main
    },
    {
        .menu_id = 2,  // 电平检测界面
        .menu_type = LIST_WITH_LOOP,
        .tab_num = 1,
        .member = menu_detect_level
    },
    {
        .menu_id = 3,  // 电平检测界面，设置界面
        .menu_type = LIST_STRING,
        .tab_num = 3,
        .member = menu_detect_level_set
    },
    {
        .menu_id = 4,  // 电平检测界面，通道设置界面
        .menu_type = LIST_STRING,
        .tab_num = 8,
        .member = menu_detect_level_channel_set
    },
    {
        .menu_id = 5,  // 系统设置界面
        .menu_type = LIST_STRING,
        .tab_num = 2,
        .member = menu_setting_list
    },
    {
        .menu_id = 6,  // I2C界面
        .menu_type = LIST_STRING,
        .tab_num = 3,
        .member = i2c_menu_list
    },
    {
        .menu_id = 7,  // I2C demo 列表
        .menu_type = LIST_STRING,
        .tab_num = 1,
        .member = i2c_demo_list
    },
};


/* =============================== */
/* function definition*/

// 初始界面
void home_func(kkui_menu_event_e event, char* tab_name, void* param)
{
    if(event == LOOP_RUN)
    {
        uiDisClear(0);
        uiDrawRBlock(16, 7, 44, 21, 7, 1);

        uiShowString(26, 34, (char*)"Kake" , 8, 1);
        uiShowString(14, 48, (char*)"MCU Tool" , 8, 1);

        uiShowString(78, 1, (char*)"CH7" , 8, 1);
        uiShowString(78, 9, (char*)"CH5" , 8, 1);
        uiShowString(78, 17, (char*)"CH3" , 8, 1);
        uiShowString(78, 25, (char*)"CH1" , 8, 1);
        uiShowString(78, 33, (char*)"DIO" , 8, 1);
        uiShowString(78, 41, (char*)"CLK" , 8, 1);
        uiShowString(78, 49, (char*)"3V3" , 8, 1);
        uiShowString(78, 57, (char*)"GND" , 8, 1);

        uiShowString(104, 1, (char*)"CH8" , 8, 1);
        uiShowString(104, 9, (char*)"CH6" , 8, 1);
        uiShowString(104, 17, (char*)"CH4" , 8, 1);
        uiShowString(104, 25, (char*)"CH2" , 8, 1);
        uiShowString(104, 33, (char*)"TXD" , 8, 1);
        uiShowString(104, 41, (char*)"RXD" , 8, 1);
        uiShowString(104, 49, (char*)"+5V" , 8, 1);
        uiShowString(104, 57, (char*)"GND" , 8, 1);

        uiDrawRBlock(76, 0, 23, 64, 3, 2);
        uiDrawRBlock(102, 0, 23, 64, 3, 2);
    }
}

///////////////////////////////////////////////

uint8_t K_LEFT_GET(int param)
{
    return (HAL_GPIO_ReadPin(BTN_L_GPIO_Port, BTN_L_Pin) == 0);
}
uint8_t K_RIGHT_GET(int param)
{
    return (HAL_GPIO_ReadPin(BTN_R_GPIO_Port, BTN_R_Pin) == 0);
}
uint8_t K_ENTER_GET(int param)
{
    return (HAL_GPIO_ReadPin(BTN_M_GPIO_Port, BTN_M_Pin) == 0);
}
uint8_t K_BACK_GET(int param)
{
    return (HAL_GPIO_ReadPin(BTN_B_GPIO_Port, BTN_B_Pin) == 0);
}

kkui_key_cfg_s key_info[] =
{
    1500, NULL, K_LEFT_GET,		// id = 0
    1500, NULL, K_RIGHT_GET,		// id = 1
    1500, NULL, K_ENTER_GET,		// id = 2
    1500, NULL, K_BACK_GET,		// id = 3
};


